package com.gticket.customerbackend.mapper;

import com.gticket.customerbackend.pojo.CinemaCode;
import com.gticket.customerbackend.pojo.CinemaHall;
import com.gticket.customerbackend.pojo.CinemaOriginalSeats;
import com.gticket.customerbackend.pojo.Cinemas;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ConsoleMapper {

    @Select("Select * from ${db}.cinema where flag = 0")
    public List<Cinemas> getAllCinemas(CinemaCode cinemaCode);

    // 根据分院id查找所有的影院座位排布
    @Select("Select cinemahall.*, originalseat.ch_capacity " +
            "from ${cinemacode.db}.cinemahall left join ${cinemacode.db}.originalseat " +
            "ON cinemahall.os_id = originalseat.os_id " +
            "where cinemahall.cinema_id = #{cinema_id} AND cinemahall.flag = 0")
    public List<CinemaHall> getCinemaHallByCid(@Param("cinemacode") CinemaCode CinemaCode, @Param("cinema_id") int cinema_id);

    @Select("Select * from ${cinemacode.db}.originalseat where os_id=#{os_id} and flag = 0")
    public CinemaOriginalSeats getCinemaOrignalSeats(@Param("cinemacode") CinemaCode cinemaCode, @Param("os_id") int os_id);

    @Update("Update ${cinemacode.db}.originalseat set row_${row_id} = #{row_cnt} where os_id = #{os_id}")
    public void updateSeats(@Param("cinemacode") CinemaCode cinemaCode, @Param("os_id") int os_id, @Param("row_id") int row_id, @Param("row_cnt") String row_cnt);

    @Update("Update ${cinemacode.db}.originalseat set ch_capacity = #{ch_capacity} where os_id = #{os_id}")
    public void updateCapacity(@Param("cinemacode") CinemaCode cinemaCode, @Param("os_id") int os_id, @Param("ch_capacity") int ch_capacity);

    // 创建座位表
    @Insert(("INSERT INTO ${cinemacode.db}.originalseat(ch_capacity,cinema_id,ch_id) VALUES (0,#{cinema_id},#{ch_id})"))
    public boolean createNewSeats(@Param("cinemacode") CinemaCode cinemaCode,@Param("cinema_id") int cinema_id, @Param("ch_id") int ch_id);

    // 根据ch_id取出os_id
    @Select("Select os_id from ${cinemacode.db}.originalseat where ch_id = #{ch_id}")
    public int getOs_idByCh_id(@Param("cinemacode") CinemaCode cinemaCode, @Param("ch_id") int ch_id);

    //
    @Select("Select cinema_id from ${cinemacode.db}.cinemahall where ch_id = #{ch_id}")
    public int getCinema_idBCh_id(@Param("cinemacode") CinemaCode cinemaCode, @Param("ch_id") int ch_id);

    // 连接座位表与影厅表
    @Update("Update ${cinemacode.db}.cinemahall SET os_id = #{os_id} where ch_id = #{ch_id}")
    public void connectOS2CH(@Param("cinemacode") CinemaCode cinemaCode, @Param("os_id") int os_id, @Param("ch_id") int ch_id);

    // 插入新厅
    @Insert("Insert into ${cinemacode.db}.cinemahall(ch_name, cinema_id) VALUES (#{ch_name}, #{cinema_id})")
    public boolean createNewCinemaHall(@Param("cinemacode") CinemaCode cinemaCode, @Param("ch_name") String ch_name, @Param("cinema_id") int cinema_id);

    // 插入新分院
    @Insert("Insert into ${cinemacode.db}.cinema(cinema_name,company_id,cinema_address) " +
            "VALUES(#{cinema_name},#{company_id},#{cinema_address})")
    public boolean createNewCinema(@Param("cinemacode") CinemaCode cinemaCode, @Param("cinema_name") String cinema_name,
                                   @Param("company_id") int company_id, @Param("cinema_address") String cinema_address);
}
